package com.zzy.utils;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * 查询 复杂模式
 */
public class SearchComplexModel {

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        HttpHost httpHost = new HttpHost("192.168.15.128", 9200, "http");
        RestClientBuilder rb = RestClient.builder(httpHost);


        RestHighLevelClient client = new RestHighLevelClient(rb);

        try {


            SearchRequest request = new SearchRequest();
            request.indices("zzyuser");

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //1、查询索引中的全部数据
            //searchSourceBuilder.query(QueryBuilders.matchAllQuery());

            //2、查询某个 属性  key = val
            // 注意name是关键字 要使用 name.keyword 代替
            // searchSourceBuilder.query(QueryBuilders.termQuery("age",30));

            //3、分页查询
//            searchSourceBuilder.query(QueryBuilders.termQuery("sex","女"));
//            int pageNo = 2; // 第多少页
//            int pageSize = 2;// 一页多少条
//            int from = (pageNo-1) * pageSize;
//            searchSourceBuilder.from(from);
//            searchSourceBuilder.size(pageSize);

            //4、查询排序
//            searchSourceBuilder.query(QueryBuilders.termQuery("sex","女"));
//            searchSourceBuilder.sort("age", SortOrder.DESC );

            //5、过滤字段
//            searchSourceBuilder.query(QueryBuilders.termQuery("sex","女"));
//            searchSourceBuilder.sort("age", SortOrder.DESC );
//            String[] excludes = {"age"};//排除 字段
//            String[] includes = {"name","sex"};//包含 字段
//            searchSourceBuilder.fetchSource(includes,excludes);

            //6、复合查询

            //6.1 and 查询
            // BoolQueryBuilder  boolQueryBuilder = QueryBuilders.boolQuery();
//            boolQueryBuilder.must(QueryBuilders.matchQuery("age","30"));
//            boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));

            //6.2  or 查询
            // BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //boolQueryBuilder.should(QueryBuilders.matchQuery("age","30"));
            //boolQueryBuilder.should(QueryBuilders.matchQuery("age","31"));
            //searchSourceBuilder.query(boolQueryBuilder);

            //6.3 范围查询
//            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
////            rangeQueryBuilder.gt(20);//  >
////            rangeQueryBuilder.lt(30); //  <
//            rangeQueryBuilder.gte(20);//  >=
//            rangeQueryBuilder.lte(30); //  <=
//            searchSourceBuilder.query(rangeQueryBuilder);

            // 6.4 模糊查询
//            FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name","五");
////            FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders
////                    .fuzzyQuery("name","五")
////                    .fuzziness(Fuzziness.ONE);
//            searchSourceBuilder.query(fuzzyQueryBuilder);


            // 6.5 高亮查询

//            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", "赵六");
//            HighlightBuilder highlightBuilder = new HighlightBuilder();
//            highlightBuilder.preTags("<font color='red'>");
//            highlightBuilder.postTags("</font>");
//            highlightBuilder.field("name.keyword");
//            searchSourceBuilder.highlighter( highlightBuilder );
//            searchSourceBuilder.query(termQueryBuilder);


            //6.6 聚合查询
//            AggregationBuilder aggregationBuilder =
//                    AggregationBuilders.max("maxAge")
//                            .field("age");
//
//            searchSourceBuilder.aggregation(aggregationBuilder);

            AggregationBuilder aggregationBuilder = AggregationBuilders
                    .terms("ageGroup") .field("age");
            searchSourceBuilder.aggregation(aggregationBuilder);





            System.out.println("----------------------分界线----------------------");
            request.source(searchSourceBuilder);
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            System.out.println("-------------111-----------");
            System.out.println(response);
            SearchHits hits = response.getHits();
            System.out.println(hits.getTotalHits());
            System.out.println(hits.getMaxScore());
            System.out.println("-------------22-----------");
            for (SearchHit hit : hits) {
                //System.out.println(hit);
                System.out.println(hit.getSourceAsString());
            }

            client.close();//关闭链接

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
